<?php

namespace App\Admin\Controllers;

use function app\Common\diffBetweenTwoDays;
use App\Http\Controllers\Controller;
use App\Models\Chat;
use App\Models\Log;
use App\Models\User;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Message\MessageModel;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Support\Facades\Input;
use Spatie\Permission\Models\Role;

class HomeController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Home');
            $content->description('Description...');
            //$content->row(Dashboard::title());
            $content->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $column->append(new InfoBox('Users', 'users', 'aqua', '/admin/public/users', User::count()));
                });
                $row->column(3, function (Column $column) {
                    $column->append(new InfoBox('Roles', 'user', 'green', '/admin/public/roles', Role::count()));
                    //$column->append(Dashboard::extensions());
                });
                $row->column(3, function (Column $column) {
                    $column->append(new InfoBox('Chats', 'comments-o', 'yellow', '/admin/public/chats', Chat::count()));
                    //$column->append(Dashboard::dependencies());
                });
                $row->column(3, function (Column $column) {
                    $column->append(new InfoBox('Messages', 'commenting-o', 'red', '/admin/messages', MessageModel::count()));
                    //$column->append(Dashboard::dependencies());
                });
            });
            $content->row(function (Row $row) {
                $row->column(6,
                    view('admin.charts.bar')
                );
                $row->column(6,
                    view('admin.charts.pie')
                );
            });
        });
    }
    public function bar(){
        $min = Chat::min('time');
        $max = Chat::max('time');
        $l=diffBetweenTwoDays($min,$max);
        $time=collect();
        $counts=collect();
        for ($i=0;$i<=$l;$i++){
            $begin=$min;
            $time->push(date('n-j',$min));
            $min=strtotime("+1 day",$min);
            $end=$min;
            $count = Chat::withTrashed()->whereBetween('time', [$begin, $end])->count();
            $counts->push($count);
        }
        return response()->json([
                'time'=>$time,
                'counts'=>$counts
            ]);
    }
    public function pie(){
        $roles = Role::all();
        $names = $roles->pluck('description');
        $combined = collect();
        foreach ($roles as $role){
            $v['value']=$role->users->count();
            $v['name']=$role->description;
            $combined->push($v);
        }
        return response()->json([
            'names'=>$names,
            'combined'=>$combined
        ]);
    }
}
